CREATE TABLE [dbo].[tb_departamentos]
(
[idDepartamento] [int] NOT NULL,
[nombre] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Provincia] [int] NOT NULL,
[idCatamarca] [int] NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updatetb_departamentos] ON [dbo].[tb_departamentos]
 FOR UPDATE AS 
 IF (update(idDepartamento)) begin 
	 SELECT * FROM inserted 
	 IF @@ROWCOUNT = 1 begin 
		 UPDATE tb_Localidades
		 SET Departamento = (SELECT idDepartamento FROM inserted)
		 WHERE tb_Localidades.Departamento = (SELECT idDepartamento FROM deleted)
	end  ELSE begin 
		 ROLLBACK TRANSACTION 
 end 
 end
GO
ALTER TABLE [dbo].[tb_departamentos] ADD CONSTRAINT [PK_tb_departamentos] PRIMARY KEY NONCLUSTERED  ([idDepartamento]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_departamentos] WITH NOCHECK ADD CONSTRAINT [FK_tb_departamentos_tb_Provincias] FOREIGN KEY ([Provincia]) REFERENCES [dbo].[tb_Provincias] ([idProvincia])
GO
